package cn.wisenergy.pai.monitor.linux.parser;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class CPU implements Serializable{
	
	private static final long serialVersionUID = 8566089910131186468L;
	//cpu 使用率
	private float cpu =0.0f;
	private float use =0.0f;
	private float iow=0.0f;
	private float sys=0.0f;
	private float nic=0.0f;
	private float idl=0.0f;
	private float irq=0.0f;
	private float sirq=0.0f;
	public float getCpu() {
		return cpu;
	}
	public void setCpu(final float cpu) {
		this.cpu = cpu;
	}
	
	public float getUse() {
		return use;
	}
	public void setUse(float use) {
		this.use = use;
	}
	public float getIow() {
		return iow;
	}
	public void setIow(float iow) {
		this.iow = iow;
	}
	public float getSys() {
		return sys;
	}
	public void setSys(float sys) {
		this.sys = sys;
	}
	
	public float getNic() {
		return nic;
	}
	public void setNic(float nic) {
		this.nic = nic;
	}
	public float getIdl() {
		return idl;
	}
	public void setIdl(float idl) {
		this.idl = idl;
	}
	public float getIrq() {
		return irq;
	}
	public void setIrq(float irq) {
		this.irq = irq;
	}
	public float getSirq() {
		return sirq;
	}
	public void setSirq(float sirq) {
		this.sirq = sirq;
	}
	@Override
	public byte[] toBytes() throws IOException{
		final ByteArrayOutputStream bytesOutput=new ByteArrayOutputStream();
		final DataOutputStream dataOutput=new DataOutputStream(bytesOutput);
		dataOutput.writeFloat(cpu);
		dataOutput.writeFloat(use);
		dataOutput.writeFloat(iow);
		dataOutput.writeFloat(sys);
		dataOutput.writeFloat(idl);
		dataOutput.writeFloat(nic);
		dataOutput.writeFloat(irq);
		dataOutput.writeFloat(sirq);
		final byte[] result=bytesOutput.toByteArray();
		dataOutput.close();
		bytesOutput.close();
		return result;
	}
	@Override
	public void toObject(final byte[] ba) throws IOException {
		final ByteArrayInputStream bytesInput=new ByteArrayInputStream(ba);
		final DataInputStream dataInput=new DataInputStream(bytesInput);
		if(dataInput.available()>0){
			cpu=dataInput.readFloat();
		}
		if(dataInput.available()>0){
			use=dataInput.readFloat();
		}
		if(dataInput.available()>0){
			iow=dataInput.readFloat();
		}
		if(dataInput.available()>0){
			sys=dataInput.readFloat();
		}
		if(dataInput.available()>0){
			idl=dataInput.readFloat();
		}
		if(dataInput.available()>0){
			nic=dataInput.readFloat();
		}
		if(dataInput.available()>0){
			irq=dataInput.readFloat();
		}
		if(dataInput.available()>0){
			sirq=dataInput.readFloat();
		}
		dataInput.close();
		bytesInput.close();
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Float.floatToIntBits(cpu);
		result = prime * result + Float.floatToIntBits(idl);
		result = prime * result + Float.floatToIntBits(iow);
		result = prime * result + Float.floatToIntBits(irq);
		result = prime * result + Float.floatToIntBits(nic);
		result = prime * result + Float.floatToIntBits(sirq);
		result = prime * result + Float.floatToIntBits(sys);
		result = prime * result + Float.floatToIntBits(use);
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CPU other = (CPU) obj;
		if (Float.floatToIntBits(cpu) != Float.floatToIntBits(other.cpu))
			return false;
		if (Float.floatToIntBits(idl) != Float.floatToIntBits(other.idl))
			return false;
		if (Float.floatToIntBits(iow) != Float.floatToIntBits(other.iow))
			return false;
		if (Float.floatToIntBits(irq) != Float.floatToIntBits(other.irq))
			return false;
		if (Float.floatToIntBits(nic) != Float.floatToIntBits(other.nic))
			return false;
		if (Float.floatToIntBits(sirq) != Float.floatToIntBits(other.sirq))
			return false;
		if (Float.floatToIntBits(sys) != Float.floatToIntBits(other.sys))
			return false;
		if (Float.floatToIntBits(use) != Float.floatToIntBits(other.use))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "CPU [cpu=" + cpu + ", use=" + use + ", iow=" + iow + ", sys="
				+ sys + ", nic=" + nic + ", idl=" + idl + ", irq=" + irq
				+ ", sirq=" + sirq + "]";
	}
	
}
